#include "head.h"

void start_work(int netFD, MYSQL *connect){

    client_t *cli_sta = (client_t *)malloc(1*sizeof(client_t));
    bzero(cli_sta, sizeof(client_t));
    //获取连接之后，开始等待对方登录或者注册
    while(1){
        int ret = login(netFD, connect, cli_sta);
        if(ret == 0){
            //表示登录成功
            break;
        }
        if(ret == 99){
            //表示用户断开连接，不想登录了
            close(netFD);
            return;
        }

    }
    //通过ID，查找用户名和所在目录id
    user_init(cli_sta, connect);
    printf("用户信息：%d, %s, %d, %s\n", cli_sta->user_ID, cli_sta->name, cli_sta->file_id, cli_sta->dir_path);

    //登录成功
    //登陆之后，该线程为客户端服务，直到对方断开
        command_t *ct = (command_t*)malloc(1*sizeof(command_t));
    while(1){
        printf("working\n");
        int ret = send(netFD, cli_sta, sizeof(client_t), MSG_NOSIGNAL);
        ERROR_CHECK(ret, -1, "send_worker.c");
        bzero(ct, sizeof(command_t));
        printf("command\n");
        ret = recv(netFD, ct, sizeof(command_t), MSG_WAITALL);
        //ERROR_CHECK(ret, -1, "recv_worker.c");
        printf("recv = %d\n", ret);
        if(ret == 0){
            //对方已经断开
            break;
        }
        if(ct->command == 8){
            break;
        }
        handling(ct, netFD, connect, cli_sta);

    }

    free(cli_sta);
    free(ct);
    close(netFD);
}

